home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / IC++ 1.0b2 / CInternetConfig.cpp next >
Text File  |  1996-06-10  |  9KB  |  398 lines

  1. /***********************************************************************\
  2.     CInternetConfig.h
  3.     
  4.     A C++ class for using Internet Config
  5.     
  6.     by Dan Crevier
  7.     
  8.     version 1.1
  9.     
  10.     Some additions by Jerry Aman and Tom Emerson
  11.  
  12. \***********************************************************************/
  13.  
  14. #include "CInternetConfig.h"
  15.  
  16. #ifndef __ICAPI__
  17. #include "ICAPI.h"
  18. #endif
  19.  
  20. #ifndef __COMPONENTS__
  21. #include <Components.h>
  22. #endif
  23.  
  24. static void ClearMem(void *start, long len);
  25.  
  26. /***********************************************************************\
  27.     CInternetConfig - constructor
  28.         If prefFile is NULL, the default preferences are used
  29. \***********************************************************************/
  30.  
  31. CInternetConfig::CInternetConfig(OSType appSig)
  32. : started(false), mappings(0), applicationSig(appSig), installed(false)
  33. {
  34.     ComponentDescription ICDescription = {'PREF', 'ICAp', 0L, 0L, 0L};
  35.     long response;
  36.  
  37.     // see if component version of IC is installed
  38.     if (Gestalt(gestaltComponentMgr,&response)==noErr)
  39.     {
  40.         // first, see if IC is installed
  41.         if (CountComponents(&ICDescription))
  42.         {
  43.             installed = true;
  44.         }
  45.     }
  46. }
  47.         
  48. /***********************************************************************\
  49.     ~CInternetConfig - destructor
  50. \***********************************************************************/
  51.  
  52. CInternetConfig::~CInternetConfig()
  53. {
  54.     Stop();
  55. }
  56.  
  57. /***********************************************************************\
  58.     Start - starts up IC
  59. \***********************************************************************/
  60.  
  61. ICError CInternetConfig::Start(FSSpec *prefFile)
  62. {
  63.     ICError err;
  64.     
  65.     err = ICStart(&inst, applicationSig); 
  66.     if (err == noErr)
  67.     {
  68.         if (prefFile == NULL) // use default prefs
  69.         {
  70.             err = ICFindConfigFile(inst, 0, NULL);
  71.         }
  72.         else
  73.         {
  74.             ICDirSpec prefDir;
  75.             
  76.             prefDir.vRefNum = prefFile->vRefNum;
  77.             prefDir.dirID = prefFile->parID;
  78.             err = ICFindUserConfigFile(inst, &prefDir);
  79.         }
  80.         if (err == noErr) // startup was successful
  81.         {
  82.              started = true;
  83.         }
  84.         else
  85.         {
  86.             // if ICStart was called, but pref couldn't be found, we
  87.             // need to call ICStop
  88.  
  89.             ICStop(inst);
  90.         }
  91.     }
  92.     return err;
  93. }
  94.  
  95. /***********************************************************************\
  96.     Stop - stop the instance
  97. \***********************************************************************/
  98.  
  99. ICError CInternetConfig::Stop()
  100. {
  101.     if (started)
  102.     {
  103.         started = false;
  104.         
  105.         return ICStop(inst);
  106.     }
  107.     return noErr;
  108. }
  109.  
  110. /***********************************************************************\
  111.     DoURL - launch the URL past in
  112. \***********************************************************************/
  113.  
  114. ICError CInternetConfig::DoURL(StringPtr theURL)
  115. {
  116.     long start = 0, end;
  117.     
  118.     if (started)
  119.     {
  120.         end = theURL[0];
  121.         return ICLaunchURL(inst, "\p", (char *)theURL+1, end, &start, &end);
  122.     }
  123.     else
  124.     {
  125.         return -1;
  126.     }
  127. }
  128.  
  129. /***********************************************************************\
  130.     MapFilename - return the map entry for the given filename
  131. \***********************************************************************/
  132.  
  133. ICError CInternetConfig::MapFilename(StringPtr name, ICMapEntry *entry)
  134. {
  135.     if (started)
  136.     {
  137.         return ICMapFilename(inst, name, entry);
  138.     }
  139.     else
  140.     {
  141.         return -1;
  142.     }
  143. }
  144.  
  145. /***********************************************************************\
  146.     MapFilename - return the map entry for the given filename
  147.         takes name as C string
  148. \***********************************************************************/
  149.  
  150. ICError CInternetConfig::MapFilename(char *name, ICMapEntry *entry)
  151. {
  152.     short i;
  153.     Str255 newName;
  154.     
  155.     // convert name to P string
  156.     for(i=0; name[i] && i<255; i++)
  157.     {
  158.         newName[i+1] = name[i];
  159.     }
  160.     newName[0] = i;
  161.     
  162.     return MapFilename(newName, entry);
  163. }
  164.  
  165. /***********************************************************************\
  166.     StartMapIteration -- starts iterating over the mappings
  167.         Note: It calls ICBegin() and FinishMapIteration calls
  168.             ICEnd(), so you have can't handle events in between
  169. \***********************************************************************/
  170.  
  171. ICError CInternetConfig::StartMapIteration(void)
  172. {
  173.     ICError err;
  174.     ICAttr            attr = 0;
  175.     
  176.     currentPos = 1;
  177.  
  178.     if (!started) return -1;
  179.     
  180.     err = ICBegin(inst, icReadOnlyPerm);
  181.     if (err != noErr) return err;
  182.     
  183.     err = ICGetPrefHandle(inst, kICMapping, &attr, &mappings);
  184.     
  185.     if (!err)
  186.     {
  187.         err = ICCountMapEntries(inst, mappings, &numMappings);
  188.     }
  189.     
  190.     if (err)
  191.     {
  192.         ICEnd(inst);
  193.     }
  194.     
  195.     return err;
  196. }
  197.  
  198. /***********************************************************************\
  199.     NextMapEntry - get the next map entry after calling
  200.         StartMapIteration.  Returns false if there are no more entries    
  201. \***********************************************************************/
  202.  
  203. Boolean CInternetConfig::NextMapEntry(ICMapEntry *entry)
  204. {
  205.     long pos;
  206.  
  207.     if (!started || mappings == NULL) return false;
  208.     
  209.     // are we done?
  210.     if (currentPos > numMappings) return false;
  211.     
  212.     ICGetIndMapEntry(inst, mappings, currentPos, &pos, entry);
  213.  
  214.     currentPos++;
  215.     
  216.     return true;
  217. }
  218.  
  219. /***********************************************************************\
  220.     FinishMapIteration -- call after doing a map iteration    
  221. \***********************************************************************/
  222.  
  223. ICError CInternetConfig::FinishMapIteration(void)
  224. {
  225.     if (!started || mappings == NULL) return -1;
  226.     
  227.     DisposeHandle(mappings);
  228.     mappings = NULL;
  229.     
  230.     return ICEnd(inst);
  231. }
  232.  
  233. /***********************************************************************\
  234.     GetFontRecord
  235.     Return ICFontRecord for specified key - key names are in "ICKeys.h"
  236. \***********************************************************************/
  237.  
  238. ICError CInternetConfig::GetFontRecord(ConstStr255Param keyStr, ICFontRecord
  239.     *fontRecord)
  240. {
  241.     ICAttr junkAttr;
  242.     long size = sizeof(ICFontRecord);
  243.     
  244.     if (started)
  245.     {
  246.         return ICGetPref(inst, keyStr, &junkAttr, (char *)fontRecord, &size);
  247.     }
  248.     else
  249.     {
  250.         return -1;
  251.     }
  252. };
  253.  
  254.  
  255.  
  256. /***********************************************************************\
  257.     GetKeysString
  258.     Return Pascal string for specified key - key names are in "ICKeys.h"
  259.     By Jerry Aman
  260. \***********************************************************************/
  261.  
  262. ICError CInternetConfig::GetKeysString(ConstStr255Param keyStr, Str255
  263.             thePStr)
  264. {
  265.     ICAttr junkAttr;
  266.     long size = 256;
  267.     
  268.     if (started)
  269.     {
  270.         return ICGetPref(inst, keyStr, &junkAttr, (char *)thePStr, &size);
  271.     }
  272.     else
  273.     {
  274.         return -1;
  275.     }
  276. };
  277.  
  278.  
  279. /***********************************************************************\
  280.     EditPreferences
  281.     No need to supply a key string to edit the default settings
  282.     By Jerry Aman
  283. \***********************************************************************/
  284.  
  285. ICError CInternetConfig::EditPreferences(ConstStr255Param key)
  286. {
  287.     if (started)
  288.     {
  289.         return ICEditPreferences(inst, key);
  290.     }
  291.     else
  292.     {
  293.         return -1;
  294.     }
  295. }
  296.  
  297. /***********************************************************************\
  298.     GetHelperFileSpec -- return the FSSpec to the latest incarnation
  299.         of the specified helper application
  300.     By Tom Emerson
  301. \***********************************************************************/
  302.  
  303. ICError CInternetConfig::GetHelperFileSpec(StringPtr key, FSSpec *theFileSpec,
  304.     OSType *creator)
  305. {
  306.     ICAttr        junkAttr;
  307.     ICAppSpec    appSpec;
  308.     long        size = sizeof(ICAppSpec);
  309.     
  310.     if (started)
  311.     {
  312.         if (ICGetPref(inst, key, &junkAttr, (char *)&appSpec, &size) == noErr)
  313.         {
  314.             if (LocateApplication(appSpec.fCreator, theFileSpec))
  315.             {
  316.                 *creator = appSpec.fCreator;
  317.                 return noErr;
  318.             }
  319.         }
  320.     }
  321.  
  322.     return -1;
  323. }
  324.  
  325. /***********************************************************************\
  326.     LocateApplication -- find the latest version of the specified
  327.         application on the user's disks
  328.     By Tom Emerson
  329. \***********************************************************************/
  330.  
  331. Boolean CInternetConfig::LocateApplication(OSType signature, FSSpec *theFileSpec)
  332. {
  333.     short    index = 0;
  334.     Boolean    found = false;
  335.     
  336.     HVolumeParam    hpb;
  337.     DTPBRec            dpb;
  338.     HFileInfo        cpb;
  339.     
  340.     FSSpec            most_recent;
  341.     unsigned long    most_recent_date = 0;
  342.     
  343.     Str63            app_name;
  344.     
  345.     do
  346.     {
  347.         ClearMem(&hpb, sizeof(HVolumeParam));
  348.         ClearMem(&dpb, sizeof(DTPBRec));
  349.         
  350.         hpb.ioVolIndex = ++index;
  351.         if (PBHGetVInfoSync((HParamBlockRec *)&hpb) != noErr)
  352.             break;
  353.         
  354.         dpb.ioVRefNum = hpb.ioVRefNum;
  355.         if (PBDTGetPath(&dpb) == noErr)
  356.         {
  357.             dpb.ioFileCreator = signature;
  358.             dpb.ioNamePtr = app_name;
  359.             
  360.             if (PBDTGetAPPLSync(&dpb) == noErr)
  361.             {
  362.                 ClearMem(&cpb, sizeof(HFileInfo));
  363.                 cpb.ioNamePtr = app_name;
  364.                 cpb.ioVRefNum = dpb.ioVRefNum;
  365.                 cpb.ioDirID = dpb.ioAPPLParID;
  366.                 
  367.                 if ((PBGetCatInfoSync((CInfoPBRec *)&cpb) == noErr) &&
  368.                     (cpb.ioFlMdDat > most_recent_date))
  369.                 {
  370.                     most_recent.vRefNum = dpb.ioVRefNum;
  371.                     most_recent.parID = dpb.ioAPPLParID;
  372.                     BlockMoveData(app_name, most_recent.name, 64);
  373.                     most_recent_date = cpb.ioFlMdDat;
  374.                 }
  375.                 
  376.                 found = true;
  377.             }
  378.         }
  379.     } while (! hpb.ioResult);
  380.     
  381.     if (found)
  382.         *theFileSpec = most_recent;
  383.         
  384.     return found;
  385. }
  386.  
  387. /***********************************************************************\
  388.     ClearMem - clear a chunk of memory
  389. \***********************************************************************/
  390.  
  391. void ClearMem(void *start, long len)
  392. {
  393.     char *p = (char *)start;
  394.     
  395.     for(long i=0; i<len; i++)
  396.         p[i] = 0;
  397. }
  398.